package com.zmn.oms.business.impl.servtype;

import com.zmn.base.price.common.constant.ItemTypeConst;
import com.zmn.coa.dubbo.impl.interfaces.order.discount.OrderDiscountRemoteService;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.consts.GlobalConsts;
import com.zmn.dms.common.dto.promotion.dio.Meter3ArtificialPromListQuery;
import com.zmn.dms.common.dto.promotion.dro.Meter3ArtificialPromDRO;
import com.zmn.oms.common.dio.quotation3.ArtificialPromDRO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.work.modify.*;
import com.zmn.oms.model.entity.serviceitem.OrderServiceItem;
import com.zmn.scm2.common.model.dro.BasePageDRO;
import com.zmn.scm2.common.model.dro.scm.engineer.warehouse.EngineerWarehouseDRO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName FinalPriceServTypeServiceImpl
 * @description: 一口价服务类型
 * @author: wangyanci
 * @create: 2022-04-20 16:25
 * @Version 1.0
 **/
@Slf4j
@Service
public class FinalPriceServTypeServiceImpl  extends AbstractServItemService {

    @Reference(version = com.zmn.coa.common.util.DubboConsts.INTERFACE_VERSION, check = false)
    protected OrderDiscountRemoteService orderDiscountRemoteService;

    // 一口价和计价器3.0逻辑通用
    @Autowired
    private QuotationThreeServTypeServiceImpl quotationThreeServTypeService;


    @Override
    List<OrderServiceItem> doFilterServiceItem(List<OrderServiceItem> serviceItems, Integer servItemType, Integer finalPrice) {
        return quotationThreeServTypeService.doFilterServiceItem(serviceItems, servItemType, finalPrice);
    }

    @Override
    ResponseDTO<BasePageDRO<EngineerWarehouseDRO>> doListPageEngineerWarehouseByQueryV2(PartQueryDTO partQueryDTO) throws OmsBaseException {
        return quotationThreeServTypeService.doListPageEngineerWarehouseByQueryV2(partQueryDTO);
    }

    @Override
    ResponseDTO<ServiceItemResultDTO> doGetServiceItem(ServiceItemQueryDTO serviceItemQueryDTO) {
        return quotationThreeServTypeService.doGetServiceItem(serviceItemQueryDTO);
    }

    @Override
    List<ServiceItemResultDTO> doListServiceItem(ServiceItemQueryDTO serviceItemQueryDTO) {
        return quotationThreeServTypeService.doListServiceItem(serviceItemQueryDTO);
    }

    @Override
    BackProductPriceResultDTO doQueryBackProductPrice(BackProductPriceQueryDTO backProductPriceQueryDTO) {
        return quotationThreeServTypeService.doQueryBackProductPrice(backProductPriceQueryDTO);
    }

    @Override
    List<Integer> doQueryServItemTypeIdList(Integer servItemType) {
        List<Integer> itemTypeIds = new ArrayList<>();
        itemTypeIds.add(ItemTypeConst.NORMAL_ITEM);
        itemTypeIds.add(ItemTypeConst.REPLACE_ITEM);
        itemTypeIds.add(ItemTypeConst.SPECIAL_ITEM);
        return itemTypeIds;
    }

    @Override
    List<ArtificialPromDRO> doQueryArtificialPromList(ArtificialPromQueryDTO query) throws OmsBaseException {

        Meter3ArtificialPromListQuery dio = Meter3ArtificialPromListQuery.builder()
                .bizType(query.getBizType())
                .channelId(query.getChannelId())
                .cityId(query.getCityId())
                .orderExtendedWarranty(query.getOrderExtendedWarranty())
                .orderTariffType(3) // 写死
                .plat(query.getPlat())
                .productId(query.getProductId())
                .build();
        // 未使用配件，不做优惠过滤
        if (Objects.equals(query.getPartUse(), GlobalConsts.NO)) {
            ResponseDTO<List<Meter3ArtificialPromDRO>> responseDTO = meter3ArtificialPromListRemoteService.listMeter3ArtificialProm(dio);
            log.info("订单[{}]获取一口价优惠 入参：{}, 出参：{}", query.getOrderId(), dio, responseDTO);
            return super.meter3ArtificialPromDRO2ArtificialPromDRO(responseDTO.getData());
        }
        // 使用配件，过滤优惠
//        dio.setOrderAmount(query.getOriginalAmount());
        dio.setUsedDiscountAmount(query.getUsedDiscountAmount());
//        log.info("获取订单最高优惠#getOrderMaxDiscount orderAmount={},orderId={}", query.getOriginalAmount(), query.getOrderId());
//        ResponseDTO<Integer> orderMaxDiscount = orderDiscountRemoteService.getOrderMaxDiscount(query.getOriginalAmount(), query.getOrderId());
//        log.info("获取订单最高优惠#getOrderMaxDiscount orderMaxDiscount={}", orderMaxDiscount);
//        dio.setMaxDiscountAmount(orderMaxDiscount.getData());

        ResponseDTO<List<Meter3ArtificialPromDRO>> responseDTO = meter3ArtificialPromListRemoteService.listMeter3ArtificialProm(dio);
        log.info("订单[{}]获取一口价优惠 入参：{}, 出参：{}", query.getOrderId(), dio, responseDTO);

        return super.meter3ArtificialPromDRO2ArtificialPromDRO(responseDTO.getData());
    }

}